# plugs/less.py
#
#

""" output cache """

__copyright__ = 'this file is in the public domain'


from gozerbot.commands import cmnds
from gozerbot.less import less
from gozerbot.aliases import aliases
from gozerbot.examples import examples
from gozerbot.plughelp import plughelp

plughelp.add('less', 'access to the bots output cache')

def shutdown():
    less.save()

def handle_less(bot, ievent):
    try:
        if len(ievent.args) == 3:
            (who, index1, index2) = ievent.args
        elif len(ievent.args) == 2:
            who = ievent.nick
            (index1, index2) = ievent.args
        else:
            who = ievent.nick
            index1 = 0
            index2 = ievent.args[0]
        index1 = int(index1)
        index2 = int(index2)
    except IndexError:
        ievent.missing('[<who>] [<index1>] <index2>')
        return
    except ValueError:
        ievent.reply('i need integers as arguments')
        return
    txt = less.get(who, index1, index2)
    if not txt:
        ievent.reply('no data available for %s %s %s' % \
(who, index1, index2))
        return
    if ievent.msg:
        bot.output(ievent.nick, txt, 'msg')
    else:
        bot.output(ievent.channel, txt, 'msg')

cmnds.add('less', handle_less, 'USER')
examples.add('less', "less [<who>] [<index1>] <index2> .. get txt from bots \
output cache", '1) less 0 2) less 0 2 3) less bart 1 0')

def handle_lesssize(bot, ievent):
    try:
        who = ievent.args[0]
    except IndexError:
        who = ievent.nick
    ievent.reply(less.size(who))

cmnds.add('less-size', handle_lesssize, 'USER')
examples.add('less-size', "show sizes of data in bot's ouput cache", \
'less-size')
